/* 初始化 */
$(function(){
		var sort = $("#sort").val();
		var state = $("#state").val();
		var type = $("#type").val();
		var url = "cashierjson?sort=" + sort + "&state=" + state +"&type=" + type;
		$('#dd').datagrid({
    		url:url,
    		collapsible:true,
    		fit:true,
			fitColumns:true,
			toolbar:'#footer',
			pagination:true,
			pageSize:20,
			pageList:[20,50,100,200],
			remoteSort:false,
			onLoadSuccess:function(data){
		        if (data.total == 0) { 
				$(this).datagrid('appendRow', { cVoucher: '<div style="text-align:center;color:red">没有相关记录！</div>' }).datagrid('mergeCells', { index: 0, field: 'cVoucher', colspan: 4 }) 
				$(this).closest('div.datagrid-wrap').find('div.datagrid-pager').hide(); 
				 $(this).datagrid('hideColumn', 'ck');
				} 
				else {
					$(this).closest('div.datagrid-wrap').find('div.datagrid-pager').show();
					$(this).datagrid('showColumn', 'ck');}
		    },
   			columns:[[
   			 	{field:'ck',checkbox:true},
   			 	{field:'iID',hidden:'true'},
				{field:'cVoucher',title:'收据号',align:'center',width:'25%',
				 formatter:function(value,row,index){
					if(row.iID == undefined)return row.cVoucher;
					return'<a href="javascript:;">'+row.cVoucher+'</a>'
				}},
				{field:'fReceivable',title:'应收款',align:'center',width:'23%'},
				{field:'fReceipt',title:'实收款',align:'center',width:'25%'},
				{field:'iCashierState',title:'审核状态',align:'center',width:'25%',
					formatter:function(value,row,index){
						if(row.iCashierState == undefined)return '异常';
						if(row.iCashierState == 0)return '未审核';
						if(row.iCashierState == 1)return '通过';
						if(row.iCashierState == 2)return '未通过';
					}
				
				}
    		]]
		});		
});

/*搜索*/
function search(){
		var cashierState = $("#cashierState").val();
		$("#dd").datagrid("load",{
			cashierState:cashierState
		})
	}
	
/*通过*/
function cashierPass(){
	var sort = $("#sort").val();
	var rows = $('#dd').datagrid('getSelections');
		var checkNum = rows.length;
		if(checkNum==0){
			$.messager.alert('错误','至少选择一项','error');
		 }else{
			var checklist = new Array();
			var checkName = [];
			checkName.push('将要通过以下审核');
			for(var i=0; i<rows.length; i++){
				
				checklist.push(rows[i].iID);
				checkName.push('<span>'+rows[i].cVoucher +'</span>');
			}
			$.messager.confirm('提示',checkName, function(r){
    		if (r){
				$.ajax({
			        url:"/mmy/cashierpass",
			        type:"post",
			        data:{
			            checkList:checklist.toString(),
						sort:sort
			        },
			        dataType:"text",
			       success:function () {
						 $("#dd").datagrid("reload");
			             $.messager.show({title:'消息',msg:'成功',timeout:3000,});
					},
			        error:function () {
			           $.messager.show({title:'消息',msg:'失败',timeout:3000,});
			        }
			    })
    		}
		});		
	}
}
/*驳回*/
function cashierFail(){
	var sort = $("#sort").val();
	var rows = $('#dd').datagrid('getSelections');
		var checkNum = rows.length;
		if(checkNum==0){
			$.messager.alert('错误','至少选择一项','error');
		 }else{
			var checklist = new Array();
			var checkName = [];
			checkName.push('将要驳回以下审核');
			for(var i=0; i<rows.length; i++){
				
				checklist.push(rows[i].iID);
				checkName.push('<span>'+rows[i].cVoucher +'</span>');
			}
			$.messager.confirm('提示',checkName, function(r){
    		if (r){
				$.ajax({
			        url:"/mmy/cashierfail",
			        type:"post",
			        data:{
			            checkList:checklist.toString(),
						sort:sort
			        },
			        dataType:"text",
			       success:function () {
						 $("#dd").datagrid("reload");
			             $.messager.show({title:'消息',msg:'成功',timeout:3000,});
					},
			        error:function () {
			           $.messager.show({title:'消息',msg:'失败',timeout:3000,});
			        }
			    })
    		}
		});		
	}
}

